<?php
/*
 * @Author: 哲学家乔布斯 <wuyangii@qq.com>
 * @Date: 2022-10-25 20:19:21
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2022-10-25 20:22:43
 * @Descripttion: 
 */


namespace app\common\model;

use think\Model;

class Bus extends Base
{
    protected $insert = ['addtime','pass'];

    //---------------自动完成----------------------
    protected function setAddtimeAttr()
    {
        return time();
    }
    public function getAddtimeTextAttr($value, $data){
        return date('Y-m-d H:i:s', $data['addtime']);
    }

    public function getImageTextAttr($value,$data){
        return $data['image'] ? cdnurl($data['image'],true) : "";
    }
    public function getLevelTextAttr($value,$data){
        return (new \app\common\model\BusLevel())->where(['id' => $data['level_id']])->value('title');
    }
    public function setPassArrt($value){
        return StrMD5($value);
    }
    public function getIsStateTextAttr($value,$data){
        $arr = ['正常','封禁'];
        return $arr[$data['is_state']];
    }
    public function getCompanyTitleAttr($value,$data){
        return $data['company_id'] == 0 ? '' : (new \app\common\model\Company())->where(['id' => $data['company_id']])->value('title');
    }
    /**
     * 通过token获取用户信息
     *
     * @param [type] $token
     * @return void
     */
    public function getToken($token)
    {
        if (empty($token)) {
            return false;
        }
        // $info = $this->where('id', 221)
     
        $info = $this->where('token',$token)
            ->field([
                'id', 'user_name','nickname','mobile','avatar','openid','is_state'
            ])
            ->find();
        if (empty($info)) {
            return false;
        }
        if ($info['is_state'] == 1) {
            return false;
        }
        return $info;
    }
    /**
     * 设置token
     *
     * @param [type] $id
     * @return void
     */
    public function setToken($id)
    {
        $account = $this->where(['id' => $id])->find();
        if (empty($account)) {
            return false;
        }
        if(!empty($account['user_name'])){
            $token_u = $account['user_name'];
        }
        if(!empty($account['openid'])){
            $token_u = $account['openid'];
        }
        $token = StrMD5($id . $token_u . '*p$h^p*');
        // $token = StrMD5($id . uniqid() . time());
        $this->where('id', $id)->update(['token' => $token]);
        return $token;
    }
    public function increase($user_id,$money){
        //积分增加
          $user = (new \app\common\model\Bus())->where(['id' => $user_id])->find();
          $yu_e = $user['integral']+$money;
          if($yu_e >=0){
               (new \app\common\model\Bus())
                                  ->where([
                                      'id' => $user_id,
                                      'integral' => $user['integral'],
                                      ])
                                  ->update([
                                      'integral' => $yu_e
                                  ]);
             (new \app\common\model\BusMoneyLog())->insert([
                'title' => '后台充值',
                'user_id' => $user_id,
                'user_id' => $user_id,
                'tpye' => 1,
                'money' => $money,
                'addtime' => time(),
             ]);                
              return [
                  'code' => 1,
                  'msg' => '成功',
              ];
          }else{
              return [    
                  'code' => 0,
                  'msg' => '失败',
              ];
          }
    }
    public function reduce($user_id,$money){
        //积分减少
        $user = (new \app\common\model\Bus())->where(['id' => $user_id])->find();
        $yu_e = $user['integral']-$money;
        if($yu_e >=0){
             (new \app\common\model\Bus())
                                ->where([
                                    'id' => $user_id,
                                    'integral' => $user['integral'],
                                    ])
                                ->update([
                                    'integral' => $yu_e
                                ]);
                                (new \app\common\model\BusMoneyLog())->insert([
                                    'title' => '后台扣除',
                                    'user_id' => $user_id,
                                    'user_id' => $user_id,
                                    'tpye' => 2,
                                    'money' => $money,
                                    'addtime' => time(),
                                 ]);  
            return [
                'code' => 1,
                'msg' => '成功',
            ];
        }else{
            return [    
                'code' => 0,
                'msg' => '失败',
            ];
        }
    }
        /**
     * 登录
     */
    public function login($params)
    {
        $data = $this->where('user_name', $params['account'])->find();
        if (empty($data)) {
            return self::errorResult('账号不存在');
        }
        if ($data['pass'] != StrMD5($params['password'])) {
            return self::errorResult('密码错误');
        }
        // if ($data['state'] == 1) {
        //     return self::errorResult('此账号被禁用');
        // }
        session('Bus', $data);
        // session('ARoleId', $data['role_id']);
        // (new \app\common\model\AgentLogPlatform())->save([
        //     'agent_id' => $data['id'],
        //     'type' => 'user',
        //     'username' => $data['usename'],
        //     'usercompany' => $data['title'],
        // ]);
        return self::successResult($data);
    }
    public function getAgentTitleAttr($value,$data){
        return (new \app\common\model\Agent())->where(['id' => $data['agent_id']])->value('nickname');
    }
    public function getProvinceAttr($value,$data){
        return (new \app\common\model\City())->where(['id' => $data['province_id']])->value(['ShortName']);
    }
    public function getAreaAttr($value,$data){
        return (new \app\common\model\City())->where(['id' => $data['area_id']])->value(['ShortName']);
    }
    public function getCityAttr($value,$data){
        return (new \app\common\model\City())->where(['id' => $data['city_id']])->value(['ShortName']);
    }
}
